/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.scrum4j.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author LOD
 */
@Entity
@Table(name = "Sprints")
public class Sprint implements Serializable
{
   private static final long serialVersionUID = 1L;
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long sprintID;

   @Column
   private String name;

   @Column
   private String team;

   @Column
   @Temporal(TemporalType.DATE)
   private Date startDate;

   @Column
   @Enumerated(EnumType.ORDINAL)
   private SprintState currentState;

   @ManyToOne
   @JoinColumn(referencedColumnName = "projectID", name = "projectID")
   private Project project;

   @OneToMany(mappedBy = "sprint", cascade = CascadeType.ALL,orphanRemoval = false, fetch = FetchType.EAGER)
   private List<UserStory> userStories;

   public Long getSprintID()
   {
      return sprintID;
   }

   public SprintState getCurrentState()
   {
      return currentState;
   }

   public void setCurrentState(SprintState currentState)
   {
      this.currentState = currentState;
   }

   public String getName()
   {
      return name;
   }

   public void setName(String name)
   {
      this.name = name;
   }

   public Date getStartDate()
   {
      return startDate;
   }

   public void setStartDate(Date startDate)
   {
      this.startDate = startDate;
   }

   public String getTeam()
   {
      return team;
   }

   public void setTeam(String team)
   {
      this.team = team;
   }

   void setProject(Project project)
   {
      this.project = project;
   }

   public Project getProject()
   {
      return project;
   }

   public List<UserStory> getUserStories()
   {
      if (userStories == null)
      {
         userStories = new LinkedList<UserStory>();
      }
      return userStories;
   }

   public void setUserStories(List<UserStory> stories)
   {
      getUserStories().clear();
      for (UserStory userStory : stories)
      {
         userStory.setSprint(this);
         getUserStories().add(userStory);
      }
   }

   public void addUserStories(UserStory... stories)
   {
      for (UserStory userStory : stories)
      {
         userStory.setSprint(this);
         getUserStories().add(userStory);
      }
   }

   public void removeUserStories(UserStory... stories)
   {
      for (UserStory userStory : stories)
      {
         userStory.setSprint(null);
         getUserStories().remove(userStory);
      }
   }
}
